package cn.skycity.common.controller;

import cn.hutool.core.util.StrUtil;
import cn.skycity.common.service.SysConfigService;
import cn.skycity.framework.core.model.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skycity.common.log.annotation.Log;
import com.skycity.common.log.enums.BusinessType;
import com.skycity.framework.uaa.pojo.entity.SysConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Stream;


/**
 * @Description
 * @Author YingBo.Dai
 * @Date 2024/3/24
 * @Version 1.0
 */
@RestController
@RequestMapping("/config")
@Slf4j
public class ConfigController {

    @Resource
    private SysConfigService sysConfigService;

    @GetMapping("/page")
    public Result<Page<SysConfig>> pageList(Page<SysConfig> pageVo, SysConfig sysConfig) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<SysConfig>()
                .like(StrUtil.isNotBlank(sysConfig.getConfigName()), SysConfig::getConfigName, sysConfig.getConfigName())
                .or()
                .like(StrUtil.isNotBlank(sysConfig.getConfigName()), SysConfig::getConfigKey, sysConfig.getConfigName())
                .orderByDesc(SysConfig::getUpdateTime)
                .orderByDesc(SysConfig::getCreateTime);
        Page<SysConfig> result = sysConfigService.page(pageVo, queryWrapper);
        return Result.success(result);
    }


    @GetMapping("/{id}")
    public Result<SysConfig> get(@PathVariable("id")Long id){
        SysConfig sysConfig = sysConfigService.getById(id); // 根据id查询
        return Result.success(sysConfig);
    }

    @PostMapping
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    public Result<Void> add(@RequestBody SysConfig sysConfig) {
        boolean result = sysConfigService.save(sysConfig);
        return Result.judge(result);
    }

    @PutMapping
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    public Result<Void> update(@RequestBody SysConfig sysConfig){
        boolean result = sysConfigService.updateById(sysConfig);
        return Result.judge(result);
    }

    /**
     * 删除参数配置
     *
     * @param ids 参数ID串
     */
    @DeleteMapping("/{id}")
    @Transactional
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    public Result<Void> delete(@PathVariable("id")String ids){
        List<Long> idArr = Stream.of(ids.split(",")).map(Long::parseLong).toList();
        sysConfigService.removeByIds(idArr);
        return Result.success();
    }


    /**
     * 根据参数键名查询参数值
     *
     * @param configKey 参数Key
     */
    @GetMapping(value = "/configKey/{configKey}")
    public Result<String> getConfigKey(@PathVariable String configKey) {
        return Result.success(sysConfigService.getConfigKey(configKey));
    }


}
